package com.wwwarehouse.taskcenter.fragment.invite_business_information;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CustomViewPagerInternal;
import com.wwwarehouse.common.custom_widget.SlideBarView;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.InviteRankFilterBean;
import com.wwwarehouse.taskcenter.bean.InviteSuccessBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @创建者 chenchao.
 * @创建时间 2017/10/11 10:36
 * @描述 ${TODO}
 */
@Route(path = TaskCenterConstant.InviteSuccessFragment)
public class InviteSuccessFragment extends BaseFragment {

    private View                       mView;
    private StateLayout                mStateLayout;
    private SlideBarView               mSlideBarView;
    private CustomViewPagerInternal    mVpContain;
    private CirclePageIndicator        mPageIndicate;
    private Map                        mInviteSuccessMap;
    private List<Fragment>             mFragmentList;
    private Bundle                     mInviteSuccessToChildBundle;
    private InviteSuccessFragmentChild mInviteSuccessFragmentChild;
    private int                        mInviterId;
    private String                     mInviterName;
    private boolean                    isDay;
    private boolean                    isWeek;
    private boolean                    isMonth;
    private boolean                    isYear;
    private InviteRankFilterBean       mMInviteRankFilterBean1;
    private InviteRankFilterBean       mMInviteRankFilterBean2;
    private InviteRankFilterBean       mMInviteRankFilterBean3;
    private InviteRankFilterBean       mMInviteRankFilterBean4;
    private InviteRankFilterBean       mMInviteRankFilterBean5;
    private InviteRankFilterBean       mMInviteRankFilterBean6;
    private String mStartTime;
    private String mEndTime;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_invite_success, container, false);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {

        mStateLayout = findView(mView, R.id.sl);
        mVpContain = findView(mView, R.id.cvpi);
        mPageIndicate = findView(mView, R.id.cpi);
        mSlideBarView = findView(mView, R.id.sbv_slide);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof InviteSuccessFragment) {
            mActivity.setTitle(R.string.invite_success);
        }
    }

    @Override
    public void requestDatas() {
        Bundle inviteSuccessBundle = getArguments();
        if (inviteSuccessBundle != null && inviteSuccessBundle.getString("InviterName") != null && inviteSuccessBundle.getString("startTime") != null && inviteSuccessBundle.getString("endTime") != null) {
            mInviterId = inviteSuccessBundle.getInt("inviterId");
            mInviterName = inviteSuccessBundle.getString("InviterName");
            mStartTime = inviteSuccessBundle.getString("startTime");
            mEndTime = inviteSuccessBundle.getString("endTime");
            //day
            if (inviteSuccessBundle.getSerializable("mInviteRankFilterBean1") != null && inviteSuccessBundle.getSerializable("mInviteRankFilterBean2") != null) {
                isDay = true;
                mMInviteRankFilterBean1 = (InviteRankFilterBean) inviteSuccessBundle.getSerializable("mInviteRankFilterBean1");
                mMInviteRankFilterBean2 = (InviteRankFilterBean) inviteSuccessBundle.getSerializable("mInviteRankFilterBean2");
                //week
            } else if (inviteSuccessBundle.getSerializable("mInviteRankFilterBean1") != null && inviteSuccessBundle.getSerializable("mInviteRankFilterBean3") != null && inviteSuccessBundle.getSerializable("mInviteRankFilterBean4") != null) {
                isWeek = true;
                mMInviteRankFilterBean1 = (InviteRankFilterBean) inviteSuccessBundle.getSerializable("mInviteRankFilterBean1");
                mMInviteRankFilterBean3 = (InviteRankFilterBean) inviteSuccessBundle.getSerializable("mInviteRankFilterBean3");
                mMInviteRankFilterBean4 = (InviteRankFilterBean) inviteSuccessBundle.getSerializable("mInviteRankFilterBean4");
                //month
            } else if (inviteSuccessBundle.getSerializable("mInviteRankFilterBean1") != null && inviteSuccessBundle.getSerializable("mInviteRankFilterBean5") != null) {
                isMonth = true;
                mMInviteRankFilterBean1 = (InviteRankFilterBean) inviteSuccessBundle.getSerializable("mInviteRankFilterBean1");
                mMInviteRankFilterBean5 = (InviteRankFilterBean) inviteSuccessBundle.getSerializable("mInviteRankFilterBean5");
                //year
            } else if (inviteSuccessBundle.getSerializable("mInviteRankFilterBean1") != null && inviteSuccessBundle.getSerializable("mInviteRankFilterBean6") != null) {
                isYear = true;
                mMInviteRankFilterBean1 = (InviteRankFilterBean) inviteSuccessBundle.getSerializable("mInviteRankFilterBean1");
                mMInviteRankFilterBean6 = (InviteRankFilterBean) inviteSuccessBundle.getSerializable("mInviteRankFilterBean6");
            }
        }
        mFragmentList = new ArrayList<>();
        mInviteSuccessMap = new HashMap();
        mInviteSuccessMap.put("inviteeStatus", 10);
        mInviteSuccessMap.put("inviterId", mInviterId);
        mInviteSuccessMap.put("page", 1);
        mInviteSuccessMap.put("size", 8);
        mInviteSuccessMap.put("sort", "update_time desc");
        mInviteSuccessMap.put("startTime", mStartTime);
        mInviteSuccessMap.put("endTime", mEndTime);
        getInviteSuccess();
    }

    private void getInviteSuccess() {
        mStateLayout.showProgressView(true);//加载时候的状态
        NoHttpUtils.httpPost(TaskCenterConstant.GETINVITEINFO, mInviteSuccessMap, mOnResponseListener, 0);
    }

    private NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int i) {
            try {
                switch (i) {
                    case 0:
                        if ("0".equals(commonClass.getCode())) {
                            InviteSuccessBean inviteSuccessBean = JSON.parseObject(commonClass.getData().toString(), InviteSuccessBean.class);
                            int total = inviteSuccessBean.getTotal();
                            if (total == 0) {
                                mStateLayout.showEmptyView(true);//请求回来为空
                                mStateLayout.setEmptyListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        getInviteSuccess();
                                    }
                                });
                            } else {
                                final int page = (total % 8 == 0) ? total / 8 : (total / 8 + 1);
                                mFragmentList.clear();
                                for (int j = 0; j < page; j++) {
                                    mInviteSuccessToChildBundle = new Bundle();
                                    newFragment(j);
                                    mInviteSuccessFragmentChild.setArguments(mInviteSuccessToChildBundle);
                                    mFragmentList.add(mInviteSuccessFragmentChild);
                                }

                                mVpContain.setAdapter(new ContractFragmentPagerAdapter(getChildFragmentManager(), mFragmentList));
                                if (page <= 6) {
                                    mPageIndicate.setVisibility(View.VISIBLE);
                                    mSlideBarView.setVisibility(View.GONE);
                                    mPageIndicate.setViewPager(mVpContain, 0);
                                } else {
                                    mPageIndicate.setVisibility(View.GONE);
                                    mSlideBarView.setVisibility(View.VISIBLE);
                                    mSlideBarView.setTotalPage(page);
                                    mSlideBarView.setOnSlideChange(new SlideBarView.OnSlideChange() {
                                        @Override
                                        public void onSlideChange(int i) {
                                            mVpContain.setCurrentItem(page, false);
                                        }
                                    });
                                    mSlideBarView.setViewPagerInternal(mVpContain);
                                }

                            }

                        } else {
                            mStateLayout.showSystemView(commonClass.getMsg(), true);
                            mStateLayout.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    getInviteSuccess();
                                }
                            });
                        }
                        break;
                }
            } catch (Exception e) {
                LogUtils.showLog(e.toString());
            }
        }

        @Override
        public void onFailed(String s, int i) {
            mStateLayout.showNetworkView(true);//网络问题
            mStateLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getInviteSuccess();
                }
            });
        }

        @Override
        public void onFinish(int i) {
            mStateLayout.showContentView();
        }
    };

    private void newFragment(int j) {
        mInviteSuccessFragmentChild = new InviteSuccessFragmentChild();
        mInviteSuccessToChildBundle.putInt("page", j + 1);
        mInviteSuccessToChildBundle.putInt("inviterId", mInviterId);
        mInviteSuccessToChildBundle.putString("mInviterName", mInviterName);
        mInviteSuccessToChildBundle.putString("startTime", mStartTime);
        mInviteSuccessToChildBundle.putString("endTime", mEndTime);
        if (isDay) {
            mInviteSuccessToChildBundle.putSerializable("mInviteRankFilterBean1", mMInviteRankFilterBean1);
            mInviteSuccessToChildBundle.putSerializable("mInviteRankFilterBean2", mMInviteRankFilterBean2);
        } else if (isWeek) {
            mInviteSuccessToChildBundle.putSerializable("mInviteRankFilterBean1", mMInviteRankFilterBean1);
            mInviteSuccessToChildBundle.putSerializable("mInviteRankFilterBean3", mMInviteRankFilterBean3);
            mInviteSuccessToChildBundle.putSerializable("mInviteRankFilterBean4", mMInviteRankFilterBean4);
        } else if (isMonth) {
            mInviteSuccessToChildBundle.putSerializable("mInviteRankFilterBean1", mMInviteRankFilterBean1);
            mInviteSuccessToChildBundle.putSerializable("mInviteRankFilterBean5", mMInviteRankFilterBean5);
        } else if (isYear) {
            mInviteSuccessToChildBundle.putSerializable("mInviteRankFilterBean1", mMInviteRankFilterBean1);
            mInviteSuccessToChildBundle.putSerializable("mInviteRankFilterBean6", mMInviteRankFilterBean6);
        }
    }
}
